/**
 * 使用 try_recv() 接收消息，并打印出来。

*/
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
#[derive(Debug)]
#[allow(dead_code)]
struct Message {
    sender: String, // 发送者标识
    content: String, // 消息内容
    order: u8, // 消息序号

}

fn main() {
    // --snip--
    let (tx, rx) = mpsc::channel();   //channel() 返回一个元组，分别表示发送和接收者
    let tx1 = tx.clone();
    let handle1=thread::spawn(move || {           // 移动tx1所有权到新线程中。为什么需要move？规定就是这样
        let vals = vec![
            Message{sender: String::from("x1"),content: String::from("风"),order:1},
            Message{sender: String::from("x1"),content: String::from("紧"),order:2},
            Message{sender: String::from("x1"),content: String::from("扯"),order:3},
            Message{sender: String::from("x1"),content: String::from("呼"),order:4}
        ];
        for val in vals {
            tx1.send(val).unwrap();
            println!("tx1 is sending....");
            thread::sleep(Duration::from_millis(200));
        }
    });

    let handle0=thread::spawn(move || {
        let vals = vec![
            Message{sender: String::from("x0"),content: String::from("乘"),order:1},
            Message{sender: String::from("x0"),content: String::from("胜"),order:2},
            Message{sender: String::from("x0"),content: String::from("追"),order:3},
            Message{sender: String::from("x0"),content: String::from("击"),order:4}
        ];

        for val in vals {
            tx.send(val).unwrap();
            println!("tx0 is sending....");
            thread::sleep(Duration::from_millis(100));
        }
    });
    handle0.join().unwrap();
    handle1.join().unwrap();
}